package com.oa.resources.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.oa.common.config.OAConfig;
import com.oa.common.core.domain.model.LoginUser;
import com.oa.common.utils.file.FileTypeUtils;
import com.oa.common.utils.file.FileUploadUtils;
import com.oa.common.utils.file.FileUtils;
import com.oa.framework.config.ServerConfig;
import com.oa.resources.utils.ExtUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.oa.common.annotation.Log;
import com.oa.common.core.controller.BaseController;
import com.oa.common.core.domain.AjaxResult;
import com.oa.common.enums.BusinessType;
import com.oa.resources.domain.OaResources;
import com.oa.resources.service.IOaResourcesService;
import com.oa.common.utils.poi.ExcelUtil;
import com.oa.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 资源管理Controller
 * 
 * @author li.ziye
 * @date 2022-05-18
 */
@RestController
@RequestMapping("/resources/resources")
public class OaResourcesController extends BaseController
{

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private IOaResourcesService oaResourcesService;

    /**
     * 查询资源管理列表
     */
    @PreAuthorize("@ss.hasPermi('resources:resources:list')")
    @GetMapping("/list")
    public TableDataInfo list(OaResources oaResources)
    {
        startPage();
        List<OaResources> list = oaResourcesService.selectOaResourcesList(oaResources);
        return getDataTable(list);
    }

    /**
     * 导出资源管理列表
     */
    @PreAuthorize("@ss.hasPermi('resources:resources:export')")
    @Log(title = "资源管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OaResources oaResources)
    {
        List<OaResources> list = oaResourcesService.selectOaResourcesList(oaResources);
        ExcelUtil<OaResources> util = new ExcelUtil<OaResources>(OaResources.class);
        util.exportExcel(response, list, "资源管理数据");
    }

    /**
     * 获取资源管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('resources:resources:query')")
    @GetMapping(value = "/{resourcesId}")
    public AjaxResult getInfo(@PathVariable("resourcesId") Long resourcesId)
    {
        return AjaxResult.success(oaResourcesService.selectOaResourcesByResourcesId(resourcesId));
    }

    /**
     * 上传资源
     */
    @Log(title = "资源管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(MultipartFile file) throws Exception
    {
        OaResources oaResources = new OaResources();
        try
        {
            // 上传文件路径
            String filePath = OAConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());

            oaResources.setResourcesName(file.getOriginalFilename());
            oaResources.setResourcesExt(FileTypeUtils.getFileType(fileName));
            oaResources.setResourcesPath(url);
            oaResources.setResourcesType(ExtUtil.getExtType(FileTypeUtils.getFileType(fileName)));
            oaResourcesService.insertOaResources(oaResources);
            ajax.put("resourcesId", oaResources.getResourcesId());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error("资源上传异常，错误信息：[ "+e.getMessage()+" ]");
        }
    }

    /**
     * 修改资源管理
     */
    @PreAuthorize("@ss.hasPermi('resources:resources:edit')")
    @Log(title = "资源管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaResources oaResources)
    {
        return toAjax(oaResourcesService.updateOaResources(oaResources));
    }

    /**
     * 删除资源管理
     */
    @PreAuthorize("@ss.hasPermi('resources:resources:remove')")
    @Log(title = "资源管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{resourcesIds}")
    public AjaxResult remove(@PathVariable Long[] resourcesIds)
    {
        return toAjax(oaResourcesService.deleteOaResourcesByResourcesIds(resourcesIds));
    }
}
